//
//  NSString+Helper.m
//  MFWSdk
//
//  Created by xiaofeng on 14-9-4.
//  Copyright (c) 2014年 Mafengwo Inc. All rights reserved.
//

#import "NSString+Helper.h"
#import "LoadableCategory.h"
MAKE_CATEGORIES_LOADABLE(MFWSdk_NSString_Helper)
#define DEAFAULTLINESPACE 5.0f
#define DEAFAULTWORDSPACE 1.0f

@implementation NSString (Helper)
- (NSString*)trim;
{
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

- (NSString *)removeAllSpace{
    NSString *strUrl = [self stringByReplacingOccurrencesOfString:@" " withString:@""];
    return strUrl;
}

- (NSMutableAttributedString*)lineSpace:(CGFloat)lineSpace andLineBreakMode:(NSLineBreakMode)lineModel{
    if ([MFWStringUtils isEmptyString:self]){
        return nil;
    }
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragrapStyle=[[NSMutableParagraphStyle alloc]init];
    paragrapStyle.lineBreakMode = lineModel;
    paragrapStyle.lineSpacing = lineSpace;
    [attString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, self.length)];
    return attString;
}

- (NSMutableAttributedString*)lineSpace:(CGFloat)lineSpace{
    if ([MFWStringUtils isEmptyString:self]){
        return nil;
    }
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragrapStyle=[[NSMutableParagraphStyle alloc]init];
    paragrapStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragrapStyle.lineSpacing = lineSpace;
    [attString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, self.length)];
    return attString;
}


- (NSMutableAttributedString*)lineSpace:(CGFloat)lineSpace textAlignment:(NSTextAlignment)alignment{
    if ([MFWStringUtils isEmptyString:self]){
        return nil;
    }
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragrapStyle=[[NSMutableParagraphStyle alloc]init];
    paragrapStyle.lineBreakMode = NSLineBreakByWordWrapping;
    paragrapStyle.lineSpacing = lineSpace;
    paragrapStyle.alignment = alignment;
    [attString addAttribute:NSParagraphStyleAttributeName value:paragrapStyle range:NSMakeRange(0, self.length)];
    return attString;
}

- (NSMutableAttributedString *)defaultLineSpaceAndWordSpace{
    if ([MFWStringUtils isEmptyString:self]){
        return nil;
    }
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragrapStyle=[[NSMutableParagraphStyle alloc]init];
    paragrapStyle.lineSpacing = DEAFAULTLINESPACE;
    paragrapStyle.alignment = NSTextAlignmentJustified;
    paragrapStyle.lineBreakMode = NSLineBreakByTruncatingTail;
    
    [attString addAttributes:@{NSKernAttributeName:@(DEAFAULTWORDSPACE),NSParagraphStyleAttributeName:paragrapStyle,NSBaselineOffsetAttributeName:@0.0f} range:NSMakeRange(0, self.length)];
    
    return attString;
}

-(NSMutableAttributedString *)wordSpace:(CGFloat)wordSpace lineSpace:(CGFloat)lineSpace{
    if ([MFWStringUtils isEmptyString:self]){
        return nil;
    }
    NSMutableAttributedString *attString=[[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragrapStyle=[[NSMutableParagraphStyle alloc]init];
    paragrapStyle.lineSpacing = lineSpace;
    paragrapStyle.lineBreakMode = NSLineBreakByWordWrapping;

    [attString addAttributes:@{NSKernAttributeName:@(wordSpace),NSParagraphStyleAttributeName:paragrapStyle} range:NSMakeRange(0, self.length)];
    
    
    return attString;
}

- (CGFloat)stringHeightLineSpace:(CGFloat)lineSpace wordSpace:(CGFloat)wordSapce fontSzie:(CGFloat)fontSize width:(CGFloat)width{
    if ([MFWStringUtils isEmptyString:self]){
        return 0;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing=lineSpace;
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle.copy,NSKernAttributeName:@(wordSapce)};
    
    CGRect rect=[self boundingRectWithSize:CGSizeMake(width, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return rect.size.height;
}

- (CGFloat)stringHeightLineSpace:(CGFloat)lineSpace  fontSzie:(CGFloat)fontSize width:(CGFloat)width{
    if ([MFWStringUtils isEmptyString:self]){
        return 0;
    }
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineSpacing=lineSpace;
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize], NSParagraphStyleAttributeName:paragraphStyle.copy};
    
    CGRect rect=[self boundingRectWithSize:CGSizeMake(width, 9999) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return rect.size.height;
}

- (CGFloat)stringWidthFontSize:(CGFloat)fontSize height:(CGFloat)height{
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping;
    NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:fontSize]};
    
    CGRect rect=[self boundingRectWithSize:CGSizeMake(9999, height) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil];
    return rect.size.width;
}

-(NSString *)getDateStringWithTime{
    
    NSTimeInterval time=[self doubleValue];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:time];
    
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy.MM.dd"];
    
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    return currentDateStr;
}

- (NSDictionary*)dictionaryFromQueryUsingEncoding:(NSStringEncoding)encoding{
    NSCharacterSet* delimiterSet = [NSCharacterSet characterSetWithCharactersInString:@"&;"];
    NSMutableDictionary* pairs = [NSMutableDictionary dictionary];
    NSScanner* scanner = [[NSScanner alloc] initWithString:self];
    while (![scanner isAtEnd]) {
        NSString* pairString = nil;
        [scanner scanUpToCharactersFromSet:delimiterSet intoString:&pairString];
        [scanner scanCharactersFromSet:delimiterSet intoString:NULL];
        NSArray* kvPair = [pairString componentsSeparatedByString:@"="];
        if (kvPair.count == 2) {
            NSString* key = [[kvPair objectAtIndex:0]
                             stringByReplacingPercentEscapesUsingEncoding:encoding];
            NSString* value = [[kvPair objectAtIndex:1]
                               stringByReplacingPercentEscapesUsingEncoding:encoding];
            [pairs setObject:value forKey:key];
        }
    }
    
    return [NSDictionary dictionaryWithDictionary:pairs];
}

- (NSUInteger)numberOfLines
{
    return [self componentsSeparatedByString:@"\n"].count + 1;
}

@end
